<?php
namespace app\banquet\controller;
use app\AdminRest;
use app\banquet\model\Order;
use app\massage\model\Article;

use app\massage\model\Cap;
use app\massage\model\CarAtvRecord;
use app\massage\model\CarDriver;
use app\massage\model\Config;
use app\massage\model\Date;

use app\massage\model\OrderGoods;
use app\massage\model\RefundOrder;
use app\massage\model\User;
use app\massage\model\Wallet;
use longbingcore\wxcore\Excel;
use think\App;
use app\massage\model\Order as Model;


class AdminExcel extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    protected $record_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Order();


    }





    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function orderExcel(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['place_title'])){

            $dis[] = ['a.place_title','like','%'.$input['place_title'].'%'];
        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];
        }

        if(!empty($input['phone'])){

            $dis[] = ['b.phone','like','%'.$input['phone'].'%'];
        }

        if(!empty($input['start_time'])){

            $dis[] = ['a.time_date','between',"{$input['start_time']},{$input['end_time']}"];
        }

        if(!empty($input['company_id'])){

            $dis[] = ['a.company_id','=',$input['company_id']];
        }

        if(!empty($input['have_eva'])){

            $dis[] = ['a.have_eva','=',$input['have_eva']];

            $input['pay_type'] = 0;

        }

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

            $dis[] = ['a.have_eva','=',0];

        }

        $order_model = new Order();

        $data = $order_model->alias('a')
            ->join('longbing_card_user_phone b','a.user_id = b.user_id','left')
            ->where($dis)
            ->field('a.*,b.phone')
            ->group('a.id')
            ->order('a.id desc')
            ->select()
            ->toArray();

//        dump($data);exit;

        if(!empty($data)){

            $user_model = new \app\card\model\User();

            foreach ($data as &$vs){

                $vs['user_info'] = $user_model->where(['id'=>$vs['user_id']])->field('nickName')->find();

                $vs['staff_name']= lbUserName($vs['staff_id']);

                $vs['week']      = changeWeek(date('w',$vs['time_date']));

                $vs['time_type'] = $vs['time_type']==1?'中午':'晚上';
            }

        }

        $name  = '场地预约列表';

        $header=[
            '订单ID',
            '宴会厅',
            '容纳桌数',
            '客户ID',
            '客户昵称',
            '客户手机号',
            '员工',
            '公司/部门',
            '预定日期',
            '预付金',
            '支付方式',
            '系统订单号',
            '商户订单号',
            '下单时间',
            '状态',
        ];

        $new_data = [];

        foreach ($data as $v){

            $info   = array();

            $info[] = $v['id'];

            $info[] = $v['place_title'];

            $info[] = $v['table_text'];

            $info[] = $v['user_id'];

            $info[] = $v['user_info']['nickName'];

            $info[] = $v['phone'];

            $info[] = $v['staff_name'];

            $info[] = $v['company_name'];

            $info[] = date('Y-m-d',$v['time_date']).'('.$v['week'].') '.$v['time_type'];

            $info[] = $v['pay_price'];

            $info[] = $v['balance']>0?'储值支付':'微信支付';

            $info[] = $v['order_code'];

            $info[] = $v['transaction_id'];

            $info[] = date('Y-m-d H:i:s',$v['create_time']);

            $info[] = $this->orderStatusText($v['pay_type']);


            $new_data[] = $info;
        }

        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-30 16:32
     * @功能说明:
     */
    public function orderStatusText($status){

        switch ($status){

            case 1:
                return '待支付';

                break;
            case 2:
                return '已完成';

                break;
            case -1:
                return '已取消';

                break;

        }

    }











}
